/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package elementsDefense.enemies;

import com.jme3.asset.AssetManager;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.AbstractControl;
import com.jme3.scene.control.Control;
import com.jme3.scene.shape.Torus;

/**
 *
 * @author Ricardo
 */
public class HealthControl extends AbstractControl{
    AssetManager assetManager;
    Node node;
    
    static Material[] materials;
    
    BasicEnemy owner;
    Geometry bar;
    
    float currentHealth=100;
    final float maxHealth=100;
    
    public HealthControl(AssetManager assetManager, Node node, BasicEnemy owner) {
        if(materials==null){
            materials = new Material[healthGradient.length];
            for (int i = 0; i < healthGradient.length; i++) {
                materials[i] = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
                materials[i].setColor("Color", healthGradient[i]);
            }
        }
        this.assetManager = assetManager;
        this.node =node;
        this.owner = owner;
        bar = new Geometry("healthbar", new Torus(10, 3, 0.05f, 0.2f));
        bar.setMaterial(materials[materials.length-1]);
        node.attachChild(bar);
        bar.rotate(FastMath.HALF_PI, 0, 0);
    }

    @Override
    public void setSpatial(Spatial spatial) {
        super.setSpatial(spatial);
        if(spatial==null){
            bar.removeFromParent();
        }
    }
    
    @Override
    protected void controlUpdate(float tpf) {
        Vector3f tmp = spatial.getLocalTranslation();
        bar.setLocalTranslation(tmp.x, 0, tmp.z);
    }

    @Override
    protected void controlRender(RenderManager rm, ViewPort vp) {
    }

    public Control cloneForSpatial(Spatial spatial) {
        final HealthControl control = new HealthControl(assetManager,node,owner);
        /* Optional: use setters to copy userdata into the cloned control */
        // control.setIndex(i); // example
        control.setSpatial(spatial);
        return control;
    }

    void subtract(float damage) {
        currentHealth-=damage;
        if(currentHealth<=0) {
            currentHealth=0;
            owner.failure();
        }else{
            float per = currentHealth/maxHealth;
            bar.setMaterial(materials[(int)(per*10)]);
        }
    }
    
    
    
    
    static ColorRGBA colr(int hex){
        return new ColorRGBA((float)((hex>>16)&0xFF)/0xFF, (float)((hex>>8)&0xFF)/0xFF, (float)((hex)&0xFF)/0xFF, 1);
    }
    static final ColorRGBA[] healthGradient=new ColorRGBA[]{
        colr(0xff0000),
        colr(0xfc2900),
        colr(0xf96700),
        colr(0xf3a900),
        colr(0xe7e200),
        colr(0xd2ff00),
        colr(0xb2ff00),
        colr(0x89ff00),
        colr(0x5eff00),
        colr(0x38ff00)
    };
}
